using System.Collections;
using TMPro;
using UnityEngine;

public class UI_Merchant : MonoBehaviour
{
    private Inventory_Player playerInventory;
    private Inventory_Merchant merchant;
    [SerializeField] private UI_ItemSlotParent inventorySlotParent;
    [SerializeField] private UI_ItemSlotParent merchantSlotParent;
    [SerializeField] private UI_EquipmentSlotParent equipmentSlotParent;

    [Header("Gold Details")]
    [SerializeField] private TextMeshProUGUI goldCount;
    public void SetupMerchantUI(Inventory_Player playerInventory, Inventory_Merchant merchantInventory)
    {
        Debug.Log("Update");
        this.playerInventory = playerInventory;
        merchant = merchantInventory;

        merchantInventory.OnInventoryChange += UpdateAllSlot;
        playerInventory.OnInventoryChange += UpdateAllSlot;

        UI_MerchantSlot[] merchantSlots = GetComponentsInChildren<UI_MerchantSlot>();

        foreach (var slot in merchantSlots)
            slot.SetupMerchantSlot(merchantInventory);
            
        UpdateAllSlot();
    }

    private void UpdateAllSlot()
    {
        inventorySlotParent.UpdateSlot(playerInventory.itemList);
        merchantSlotParent.UpdateSlot(merchant.itemList);
        equipmentSlotParent.UpdateEquipmentSlot(playerInventory.equipList);
        
        goldCount.text = playerInventory.gold.ToString();
    }
}
